package cs757;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;

import javax.swing.*;
import javax.swing.GroupLayout.Alignment;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.border.EmptyBorder;

public class DlgAddProject extends JDialog {

	private static final long serialVersionUID = -4953887834295530594L;
	
	private final JPanel contentPanel = new JPanel();
	private JLabel lblProjectNumber;
	private JTextField textFieldProjectNumber;
	private JTextField textFieldStartDay;
	private JTextField textFieldStartYear;
	private JTextField textFieldSponsorName;
	private JPanel buttonPane;
	private JLabel labelFunding;
	private JTextField textFieldFunding;
	private JTextField textFieldEndYear;
	private JLabel label_4;
	private JTextField textFieldEndDay;
	private JLabel label_5;
	private JTextField textFieldEndMonth;
	private JTextField textFieldStartMonth;
	private JLabel lblmmddyyyyEg;
	private JLabel labelmmddyyyy2;

	//Database connection
	private DatabaseConnector conn;
	private MainGUI mainGUI;
	
	private boolean ok = false;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {
			DlgAddProject dialog = new DlgAddProject();
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the dialog.
	 */
	public DlgAddProject() {
		setTitle("Add Project");
		setBounds(100, 100, 514, 281);
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		{
			lblProjectNumber = new JLabel("Project Number:");
		}
		textFieldProjectNumber = new JTextField();
		textFieldProjectNumber.setColumns(10);
		JLabel label = new JLabel("-");
		textFieldStartDay = new JTextField();
		textFieldStartDay.setColumns(10);
		JLabel label_1 = new JLabel("-");
		textFieldStartYear = new JTextField();
		textFieldStartYear.setColumns(10);
		JLabel lblSponsorName = new JLabel("Sponsor Name:");
		JLabel lblStartDate = new JLabel("Start Date:");
		JLabel lblEndDate = new JLabel("End Date:");
		textFieldStartMonth = new JTextField();
		textFieldStartMonth.setColumns(10);
		textFieldSponsorName = new JTextField();
		textFieldSponsorName.setColumns(10);
		
		labelFunding = new JLabel("Funding:");
		
		textFieldFunding = new JTextField();
		textFieldFunding.setColumns(10);
		
		textFieldEndYear = new JTextField();
		textFieldEndYear.setColumns(10);
		
		label_4 = new JLabel("-");
		
		textFieldEndDay = new JTextField();
		textFieldEndDay.setColumns(10);
		
		label_5 = new JLabel("-");
		
		textFieldEndMonth = new JTextField();
		textFieldEndMonth.setColumns(10);
		
		lblmmddyyyyEg = new JLabel("MM-DD-YYYY, e.g. 10-20-2020");
		
		labelmmddyyyy2 = new JLabel("MM-DD-YYYY, e.g. 10-20-2020");
		GroupLayout gl_contentPanel = new GroupLayout(contentPanel);
		gl_contentPanel.setHorizontalGroup(
			gl_contentPanel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPanel.createSequentialGroup()
					.addGap(11)
					.addGroup(gl_contentPanel.createParallelGroup(Alignment.LEADING)
						.addComponent(lblStartDate)
						.addComponent(lblSponsorName)
						.addComponent(lblProjectNumber)
						.addComponent(labelFunding)
						.addComponent(lblEndDate))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(gl_contentPanel.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_contentPanel.createSequentialGroup()
							.addGroup(gl_contentPanel.createParallelGroup(Alignment.LEADING)
								.addGroup(gl_contentPanel.createSequentialGroup()
									.addGroup(gl_contentPanel.createParallelGroup(Alignment.LEADING, false)
										.addComponent(textFieldEndMonth, 0, 0, Short.MAX_VALUE)
										.addComponent(textFieldStartMonth, GroupLayout.DEFAULT_SIZE, 30, Short.MAX_VALUE))
									.addPreferredGap(ComponentPlacement.RELATED)
									.addGroup(gl_contentPanel.createParallelGroup(Alignment.LEADING)
										.addComponent(label)
										.addComponent(label_5, GroupLayout.PREFERRED_SIZE, 4, GroupLayout.PREFERRED_SIZE))
									.addPreferredGap(ComponentPlacement.RELATED)
									.addGroup(gl_contentPanel.createParallelGroup(Alignment.LEADING)
										.addComponent(textFieldStartDay, GroupLayout.PREFERRED_SIZE, 26, GroupLayout.PREFERRED_SIZE)
										.addComponent(textFieldEndDay, GroupLayout.PREFERRED_SIZE, 26, GroupLayout.PREFERRED_SIZE))
									.addPreferredGap(ComponentPlacement.RELATED)
									.addGroup(gl_contentPanel.createParallelGroup(Alignment.LEADING)
										.addComponent(label_1, GroupLayout.PREFERRED_SIZE, 4, GroupLayout.PREFERRED_SIZE)
										.addComponent(label_4, GroupLayout.PREFERRED_SIZE, 4, GroupLayout.PREFERRED_SIZE))
									.addPreferredGap(ComponentPlacement.RELATED)
									.addGroup(gl_contentPanel.createParallelGroup(Alignment.LEADING, false)
										.addComponent(textFieldEndYear, 0, 0, Short.MAX_VALUE)
										.addComponent(textFieldStartYear, GroupLayout.DEFAULT_SIZE, 54, Short.MAX_VALUE)))
								.addComponent(textFieldSponsorName, GroupLayout.PREFERRED_SIZE, 188, GroupLayout.PREFERRED_SIZE)
								.addComponent(textFieldProjectNumber, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
							.addPreferredGap(ComponentPlacement.RELATED)
							.addGroup(gl_contentPanel.createParallelGroup(Alignment.LEADING, false)
								.addComponent(lblmmddyyyyEg, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
								.addComponent(labelmmddyyyy2, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
							.addGap(429))
						.addGroup(gl_contentPanel.createSequentialGroup()
							.addComponent(textFieldFunding, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
							.addContainerGap())))
		);
		gl_contentPanel.setVerticalGroup(
			gl_contentPanel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPanel.createSequentialGroup()
					.addContainerGap()
					.addGroup(gl_contentPanel.createParallelGroup(Alignment.BASELINE)
						.addComponent(textFieldProjectNumber, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(lblProjectNumber))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(gl_contentPanel.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblSponsorName)
						.addComponent(textFieldSponsorName, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(gl_contentPanel.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_contentPanel.createParallelGroup(Alignment.BASELINE)
							.addComponent(textFieldStartMonth, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
							.addComponent(lblStartDate)
							.addComponent(label)
							.addComponent(textFieldStartDay, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
							.addComponent(label_1)
							.addComponent(lblmmddyyyyEg)
							.addComponent(textFieldStartYear, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
						.addGroup(gl_contentPanel.createSequentialGroup()
							.addGap(23)
							.addGroup(gl_contentPanel.createParallelGroup(Alignment.BASELINE)
								.addComponent(textFieldEndMonth, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
								.addComponent(label_5)
								.addComponent(textFieldEndDay, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
								.addComponent(label_4)
								.addComponent(labelmmddyyyy2)
								.addComponent(textFieldEndYear, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
								.addComponent(lblEndDate))))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(gl_contentPanel.createParallelGroup(Alignment.BASELINE)
						.addComponent(textFieldFunding, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(labelFunding))
					.addContainerGap(196, Short.MAX_VALUE))
		);
		contentPanel.setLayout(gl_contentPanel);
		{
			buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			{
				JButton okButton = new JButton("OK");
				okButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent arg0) {
						try {
							addRecord();
							ok = true;
							closeDialog();
						} catch (SQLException e1) {
							JOptionPane.showMessageDialog(null, "Error inserting into database.\n Please check all fields for valid values.",
									"Error In Insert", JOptionPane.ERROR_MESSAGE);
						}
						 catch (NumberFormatException e2) {
								JOptionPane.showMessageDialog(null, "Error inserting into database.\n You appear to have entered non-integer values in integer fields.",
										"Error In Insert", JOptionPane.ERROR_MESSAGE);
						}
					}
				});
				okButton.setActionCommand("OK");
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
			{
				JButton cancelButton = new JButton("Cancel");
				cancelButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent arg0) {
						closeDialog();
					}
				});
				cancelButton.setActionCommand("Cancel");
				buttonPane.add(cancelButton);
			}
		}
		GroupLayout groupLayout = new GroupLayout(getContentPane());
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.TRAILING)
				.addComponent(contentPanel, GroupLayout.PREFERRED_SIZE, 506, Short.MAX_VALUE)
				.addGroup(Alignment.LEADING, groupLayout.createSequentialGroup()
					.addContainerGap()
					.addComponent(buttonPane, GroupLayout.DEFAULT_SIZE, 486, Short.MAX_VALUE)
					.addContainerGap())
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addComponent(contentPanel, GroupLayout.PREFERRED_SIZE, 193, GroupLayout.PREFERRED_SIZE)
					.addGap(18)
					.addComponent(buttonPane, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
					.addGap(218))
		);
		getContentPane().setLayout(groupLayout);
	}
	
	/**
	 * Gather the needed data from the dialog, and use the DatabaseConnector to execute an add statement
	 * Add a project to the projects table
	 * Assume user is the PI
	 * 
	 * @throws SQLException
	 * @throws NumberFormatException
	 */
	private void addRecord() throws SQLException, NumberFormatException{
		Object[] values = new Object[]{
			Integer.parseInt(textFieldProjectNumber.getText()),
			textFieldSponsorName.getText(),
			textFieldEndYear.getText()+"-"+textFieldEndMonth.getText()+"-"+textFieldEndDay.getText(),
			textFieldStartYear.getText()+"-"+textFieldStartMonth.getText()+"-"+textFieldStartDay.getText(),
			Float.parseFloat(textFieldFunding.getText())
		};
		conn.executeInsert(DatabaseConnector.INSERT_PROJECT, values);
		
		//Professor can only "create a record of a project which he/she is a PI," 
		//so automatically make the professor creating the record the project's manager
//		String userID = "746936514";
		String userID = mainGUI.getUserID();
		Object[] values2 = new Object[]{
				Integer.parseInt(textFieldProjectNumber.getText()),
				userID
			};
		conn.executeInsert(DatabaseConnector.INSERT_GROUP_MEMBER_PROFESSOR_MANAGER, values2);
	}

	/**
	 * Close the add student GUI
	 */
	private void closeDialog(){
		this.setVisible(false);
	}

	/**
	 * @return the ok
	 */
	public boolean isOk() {
		return ok;
	}

	/**
	 * @param conn the conn to set
	 */
	public void setConn(DatabaseConnector conn) {
		this.conn = conn;
	}
	
	/**
	 * @param conn the mainGUI to set
	 */
	public void setMainGUI(MainGUI mainGUI) {
		this.mainGUI = mainGUI;
	}
}
